package org.foreningsgatan.genetics.racers;

import java.awt.geom.Point2D;

public class CommonMath {
	protected static boolean within(double value, double min, double max) {
		return (value >= min) && (value <= max);
	}

	protected static double wrap(double value, double min, double max) {
		while (value < min) {
			value += (max - min);
		}
		
		while (value > max) {
			value -= (max - min);
		}
		
		return value;
	}

	protected static double limit(double value, double min, double max) {
		if (value < min) {
			return min;
		}
		
		if (value > max) {
			return max;
		}
		
		return value;
	}
	
	protected static double angle(Point2D p1, Point2D p2, Point2D pivot) {
		double a1 = angle(p1, pivot);
		double a2 = angle(p2, pivot);
		
		return wrap(a2 - a1, -Math.PI, Math.PI);
	}
	
	protected static double angle(Point2D p1, Point2D p2) {
		double dy = p1.getY() - p2.getY();
		double dx = p1.getX() - p2.getX();
		
		return Math.atan2(dy, dx);
	}
}
